package com.foolishbird.sdk.ui.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.foolishbird.sdk.params.bean.VerifyType;
import com.foolishbird.sdk.ui.activity.UserLoginActivity;
import com.foolishbird.sdk.ui.view.DialogFactory;
import com.foolishbird.sdk.utils.ResUtils;
import com.foolishbird.sdk.utils.ToolsUtils;


public class FoolishBirdBindPhoneFragment extends PhoneVerifyFragment implements View.OnClickListener {

    private static final String NEXT = "isNext";

    private EditText mBindPhoneNumberEt;
    private EditText mVerifyCodeEt;
    private Button mSendVerifyCodeBtn;
    private Button mBindPhoneBtn;
    private ImageView mCloseWindowBtn;

    public static FoolishBirdBindPhoneFragment newInstance(boolean isNext) {
        Bundle bundle = new Bundle();
        bundle.putBoolean(NEXT, isNext);

        FoolishBirdBindPhoneFragment fragment = new FoolishBirdBindPhoneFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof UserLoginActivity) {
            ((UserLoginActivity) context).setOnPhoneVerifyCodeListener(this);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        int resource = ResUtils.getLayoutIdentifier(getActivity(), "fb_fragment_bind_phone");
        View contentView = inflater.inflate(resource, null);

        mBindPhoneNumberEt = contentView.findViewById(ResUtils.getViewIdentifier(getActivity(), "fb_fragment_login_username_edit"));
        mVerifyCodeEt = contentView.findViewById(ResUtils.getViewIdentifier(getActivity(), "fb_fragment_login_password_edit"));
        mSendVerifyCodeBtn = contentView.findViewById(ResUtils.getViewIdentifier(getActivity(), "fb_fragment_login_code_send"));
        mBindPhoneBtn = contentView.findViewById(ResUtils.getViewIdentifier(getActivity(), "fb_fragment_login_login_btn"));
        mCloseWindowBtn = contentView.findViewById(ResUtils.getViewIdentifier(getActivity(), "fb_fragment_login_close"));
        mCloseWindowBtn.setOnClickListener(this);
//        mBindPhoneNumberEt.addTextChangedListener(this);
//        mVerifyCodeEt.addTextChangedListener(this);
        mSendVerifyCodeBtn.setOnClickListener(this);
        mBindPhoneBtn.setOnClickListener(this);
        mBindPhoneNumberEt.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int keyCode, KeyEvent event) {
                if(keyCode ==  EditorInfo.IME_ACTION_NEXT&&getSendVerifyCodeView().isClickable()){
                    sendPhoneVerifyCode();
                }
                Log.e("OnKeyListener",keyCode+".."+(event==null));
                return false;
            }
        });

        mVerifyCodeEt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if(actionId == EditorInfo.IME_ACTION_DONE&&mBindPhoneBtn.isEnabled()){
                    doBindCellPhoneNumber();
                }
                Log.e("CodeEtOnKeyListener",actionId+".."+(event==null));
                return false;
            }
        });

//        if (getArguments().getBoolean(NEXT, false)) {
//            mBindPhoneBtn.setText(getString(ResUtils.getStringIdentifier(getActivity(), "hnssn_sdk_login_cellphone_next")));
//        } else {
//            mBindPhoneBtn.setText(getString(ResUtils.getStringIdentifier(getActivity(), "hnssn_bindphone_sumbit_btn")));
//        }

        return contentView;
    }

    @Override
    protected Button getSendVerifyCodeView() {
        return mSendVerifyCodeBtn;
    }

    @Override
    protected EditText getEditorVerifyCodeView() {
        return mVerifyCodeEt;
    }




    @Override
    public void onClick(View view) {
        if (view == mSendVerifyCodeBtn) {
            sendPhoneVerifyCode();
        } else if (view == mBindPhoneBtn) {
            doBindCellPhoneNumber();
        }else if (view == mCloseWindowBtn){
            finishActivity();
        }
    }
    private void finishActivity() {
        FoolishBirdBindDialogFragment.OnRealNameVerifyListener listener = getOnRealNameVerifyListener();
        if (listener != null) {
            listener.doFinishActivity(this);
        }
    }



    private void sendPhoneVerifyCode() {
        String phoneNumber = mBindPhoneNumberEt.getText().toString().trim();

        if (TextUtils.isEmpty(phoneNumber)) {
            String message = getString(ResUtils.getStringIdentifier(getActivity(), "hnssn_hint_telephonenumber"));
            DialogFactory.createConfirmDialog(getActivity(), message).show();
            return;
        }

        if (!ToolsUtils.checkCellphone(phoneNumber)) {
            String message = getString(ResUtils.getStringIdentifier(getActivity(), "hnssn_hint_phonecurrent"));
            DialogFactory.createConfirmDialog(getActivity(), message).show();
            return;
        }
        if (getActivity() instanceof UserLoginActivity) {
            ((UserLoginActivity) getActivity()).setOnPhoneVerifyCodeListener(this);
        }
        requestVerifyCode(phoneNumber, VerifyType.PHONE_VERIFY);
    }

    private void doBindCellPhoneNumber() {
        String phoneNumber = mBindPhoneNumberEt.getText().toString().trim();
        String verifyCode = mVerifyCodeEt.getText().toString().trim();

        if (TextUtils.isEmpty(phoneNumber)) {
            String message = getString(ResUtils.getStringIdentifier(getActivity(), "hnssn_hint_telephonenumber"));
            DialogFactory.createConfirmDialog(getActivity(), message).show();
            return;
        }

        if (!ToolsUtils.checkCellphone(phoneNumber)) {
            String message = getString(ResUtils.getStringIdentifier(getActivity(), "hnssn_hint_phonecurrent"));
            DialogFactory.createConfirmDialog(getActivity(), message).show();
            return;
        }

        if (!ToolsUtils.checkNumber(verifyCode)) {
            String message = getString(ResUtils.getStringIdentifier(getActivity(), "hnssn_hint_captcha"));
            DialogFactory.createConfirmDialog(getActivity(), message).show();
            return;
        }

        FoolishBirdBindDialogFragment.OnRealNameVerifyListener listener = getOnRealNameVerifyListener();
        if (listener != null) {
            listener.doBindOrRealNameVerify(phoneNumber, verifyCode, null, null);
        }
    }

    private FoolishBirdBindDialogFragment.OnRealNameVerifyListener getOnRealNameVerifyListener() {
        Activity activity = getActivity();
        if (activity instanceof FoolishBirdBindDialogFragment.OnRealNameVerifyListener) {
            return (FoolishBirdBindDialogFragment.OnRealNameVerifyListener) activity;
        }
        return null;
    }
}
